(function() {
  var HttpHandler, ViewContentHandler, ViewDataHandler, comodityHandler, _;

  _ = require('underscore');

  ViewContentHandler = require('../helpers/view_content_handler');

  HttpHandler = require('../../../helpers/http_handler');

  ViewDataHandler = require('../models/view_data_handler');

  comodityHandler = {
    commodityPageHandler: function(req, res, next) {
      var id;
      id = req.params.id;
      return ViewDataHandler.commodity(id, function(err, data) {
        var renderData, view;
        if (err) {
          return res.render('error', 504);
        } else {
          view = 'ys/commodity';
          renderData = ViewContentHandler.commodity(req, view, '每天浏览一下，关注更多的新产品！', -1, data[0]);
          return HttpHandler.renderAndCache(req, res, view, renderData);
        }
      });
    },
    commodityModifyPageHandler: function(req, res, next) {
      var id;
      id = req.params.id;
      return ViewDataHandler.commodity(id, function(err, data) {
        var renderData, view;
        console.dir(data);
        if (err) {
          return res.render('error', 504);
        } else {
          view = 'ys/commodity_modify';
          renderData = ViewContentHandler.commodityModify(req, view, '每天浏览一下，关注更多的新产品！', -1, data[0]);
          return HttpHandler.renderAndCache(req, res, view, renderData);
        }
      });
    },
    saveCommodityHandler: function(req, res, next) {
      var data, id;
      data = req.body;
      id = data._id;
      delete data._id;
      data.modifiedTime = new Date();
      data.price = data.price || 0;
      console.dir(data);
      return ViewDataHandler.commodityModify(id, data, function(err, data) {
        console.log("err:" + err);
        if (err) {
          data = {
            code: 1000,
            msg: err.toString()
          };
        } else {
          data = {
            code: 0,
            msg: 'success'
          };
        }
        return res.send(JSON.stringify(data));
      });
    },
    table: function(req, res, next) {
      var view;
      view = 'ys/commodity_table_view';
      return ViewDataHandler.table(function(err, data) {
        var docs, renderData, responData;
        if (err) {
          return res.render('error', 504);
        } else {
          docs = [];
          _.each(data, function(item) {
            var pickItem;
            pickItem = {
              id: item.barcode,
              cell: _.pick(item, 'barcode', 'title', 'specification')
            };
            return docs.push(pickItem);
          });
          if (req.xhr) {
            responData = {
              page: 1,
              total: docs.length,
              rows: docs
            };
            return res.json(responData);
          } else {
            renderData = ViewContentHandler.table(req, view, '每天浏览一下，关注更多的新产品！', 3, docs);
            return HttpHandler.renderAndCache(req, res, view, renderData);
          }
        }
      });
    }
  };

  module.exports = comodityHandler;

}).call(this);
